/**   
 *  
 * @ProjectName:  [acceptance] 
 * @Package:      [com.global.acceptance.web.user.UserController.java]  
 * @ClassName:    [UserController]   
 * @Description:  [一句话描述该类的功能]   
 * @Author:       [Joly]   
 * @CreateDate:   [2013年12月3日 上午10:09:29]   
 * @UpdateUser:   [Joly]   
 * @UpdateDate:   [2013年12月3日 上午10:09:29]   
 * @UpdateRemark: [说明本次修改内容]  
 * @Version:      [v1.0] 
 *    
 */
package com.global.test.web.user;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.global.domain.User;
import com.global.emc.core.exception.impl.BusinessEMCException;
import com.global.emc.core.exception.impl.ValidationEMCException;
import com.global.emc.spring.method.support.chain.BCMChain;
import com.global.emc.spring.web.bind.annotation.ModelAndBody;
import com.global.test.service.user.UserService;

@Controller
public class UserController {
	
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value="initAddUser")
	public ModelAndView initAddUser() throws BusinessEMCException, ValidationEMCException{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("AddUser");
		return mv;
	}
	
	@RequestMapping(value="initLogin")
	public ModelAndView initLogin() throws BusinessEMCException, ValidationEMCException{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("Login");
		return mv;
	}
	
	@BCMChain(ref = "loginChain")
	public ModelAndView login() throws BusinessEMCException, ValidationEMCException{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("Main");
		return mv;
	}
	
	@RequestMapping(value="addUser")
	@BCMChain(ref = "registerUserChain")
	public ModelAndView addUser(@ModelAndBody @Valid User user) throws BusinessEMCException, ValidationEMCException{
		userService.add(user);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("User");
		mv.addObject(user);
		return mv;
	}
	
	@RequestMapping(value="deleteUser")
	public void deleteUser(@RequestParam("id") int id){
		User user = new User();
		user.setId(id);
		userService.remove(user);
		
		System.out.println("usre:===================>:"+id);
	}
	
	@RequestMapping(value="updateUser")
	public void updateUser(@RequestBody User user){
		userService.update(user);
		System.out.println("usre===================>:"+user);
	}
	
	@RequestMapping(value="getUser")
	public ModelAndView getUser(@RequestParam("username") String username) throws ValidationEMCException, BusinessEMCException{
		User user = new User();
		user.setUsername(username);;
		
		user=userService.getUserByUsername(username);
		
		ModelAndView mv = new ModelAndView();
			mv.addObject(user);
		
		return mv;
	}
	
	

}
